﻿<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>老师做的计算器</title>

    <script type="text/javascript">
		var exp = "";
		function myfun(v) {
            v = v.trim();
           // alert(v);
            if (v == "=") {
                exp = exp.replace(/sin/g,"Math.sin"); //正则表达式
				myresult.value = eval(exp);
            }
            else if (v == "C") { exp = ""; myresult = ""; }
			else if (v == "sin") { exp = exp + "sin("; myresult.value = exp;}
			else {
				exp = exp + v;
				myresult.value = exp;

			}
        }


        function mykeypress(event) {
           var e = event || window.event || arguments.callee.caller.arguments[0];
            var keycode = e && e.keyCode;
            var keyChar = String.fromCharCode(keycode); //将数字形式的键值转化为真实的按键
            //alert(keycode + " keyChar= " + keyChar);
            if (window.event.shiftKey && keycode == 57) { keyChar = "("; }  //asc 57  ===>'9'
            else if (window.event.shiftKey && keycode == 48) { keyChar = ")"; }  //asc 48  ===>'0'
            else if (window.event.shiftKey && keycode == 187) { keyChar = "+"; }
            else if (window.event.shiftKey && keycode == 56) { keyChar = "*"; }
            else if (keycode == 187) { keyChar = "="; }
            else if (keycode == 189) { keyChar = "-"; }
            else if (keycode == 190) { keyChar = "."; }
            else if (keycode == 191) { keyChar = "/"; }
            else { keyChar = keyChar; }
            //alert(keycode + " keyChar= " + keyChar);

            if (keycode == 27) { exp = ""; myresult.value = ""; } // 按 Esc，相当于按下了C,复位
            if (keycode == 113) { } // 按 F2
            if (keycode == 8) {  //按下backspace
                exp = exp.substring(0, exp.length - 1);
                myresult.value = exp;
            }
            if (keycode == 13 || keyChar == "=") { myfun("="); return; }// var res = eval(exp); myresult.value = res; exp = ""; return;}// enter 键，相当于按下了=
            if ((keycode >= 48 && keycode <= 57) || keyChar == "." || keyChar == "+" || keyChar == "-" || keyChar == "*" || keyChar == "/" || keyChar == "(" || keyChar == ")") {
                exp = exp + keyChar;
                myresult.value = exp;
            }


        }
    </script>

    <style>
        input button {
            width: 50pt;
        }
    </style>
</head>



<body onkeypress="mykeypress();">

    <div align="center">

        <table border="1">
            <tr>
                <td colspan="4">&nbsp;&nbsp;&nbsp; 计算器</td>

            </tr>
            <tr>
                <td colspan="4"><input id="result" type="text" value=" " readonly size=" " /></td>
            </tr>

            <script type="text/javascript">
        var butval = new Array("0", "1", "2", "+", "3", "4", "5", "-", "6", "7", "8", "*", "9", ".", "(", "/", ")", "sin", "C", "=");
        var k = -1;
        for (var i = 0; i < 5; i++) {
            document.write("<tr>");
            for (j = 0; j < 4; j++) {
                k++;
             document.write('<td>　<input type="button" value=" '+butval[k]+' " onclick="javascript:myfun(this.value);"   /> </td>');
            }
            document.write("</tr>");
        }
            </script>
            <input id="Button1" type="button" accesskey="B" value="button(B)" onclick="alert('OK');" />
        </table>


    </div>

</body>

</html>
<script>
    var myresult = document.getElementById("result");
</script>